#include <stdlib.h>
#include <stdio.h>
#include "thumbnail_packer.h"
#include "debug.h"
#include <string.h>

void packer_usage (char * name) {
	if (!name) 
		error("Hey!! How can name be NULL!! Back to study C again!!\n");
	printf("This application allows you to package the thumbnails into a EET file with categories tags.\n");
	printf("Usage: %s [options] <Directory>\n",name);
	printf("   -o filename		Output filename, default \"thumbnail.eet\"\n");
	exit(EXIT_SUCCESS);
}


Ecore_Evas * ee=NULL;
Evas 	   * evas=NULL;

int my_init() {
	if (!ecore_init() || !ecore_evas_init()) {
		error("Failed to init\n");
		return 0;
	}
	ee = ecore_evas_buffer_new(1, 1);
	if (!ee) {
		error("Failed to initial ecore_evas\n");
		return 0 ;
	}
	evas = ecore_evas_get(ee);
	if (!evas) {
		error("Failed to initial evas\n");
		return 0 ;
	}
	return 1;
}
int main (int args, char ** argv) {
	Eet_File *ef;
	char *filename="thumbnail.eet";
	char *directory;
	if (args < 2) {
		packer_usage(argv[0]);
	}
	if (strncmp(argv[1],"-o",sizeof("-o"))==0 && args > 2) {
		filename = argv[2];
		directory = argv[3];
	} else {
		directory = argv[1];
	}

	if (!my_init()) {
		return EXIT_FAILURE;
	}
	ef = eet_open(filename, EET_FILE_MODE_WRITE);
	if (!ef) {
		error("failed to open eet\n");
		return EXIT_FAILURE;
	}
	if (!eet_init()) {
		error("Failed to initial eet\n");
		return 0 ;
	}
	traverse(ef,directory);	
	eet_close(ef);
	eet_shutdown();
	ecore_evas_free(ee);
	ecore_evas_shutdown();
	ecore_shutdown();	
	return EXIT_SUCCESS;
}
